/***************************************************************************\
 * Copyright (C) by Keio University
 * DepthImage.hpp created in 01 2013.
 * Mail : fdesorbi@hvrl.ics.keio.ac.jp
 *
 * DepthImage.hpp is part of the HVRL Engine Library.
 *
 * The HVRL Engine Library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * The HVRL Engine Library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
\***************************************************************************/


#ifndef HVRL_DEPTH_IMAGE_HPP__
#define HVRL_DEPTH_IMAGE_HPP__

#include <hvrl/datatypes/Size.hpp>

namespace hvrl{

class DepthImage {

public:

	DepthImage(void): depth(0), s(0,0), max_(0) {};

	DepthImage(const Size &s_, const short unsigned int* data = 0): depth(0), s(s_), max_(0) {

		assert(s.width > 0 && s.height > 0);
		allocateMemory();
		if(data != 0){
			setData(data);
		}
	};

	~DepthImage(void) { freeMemory(); }

	Size getSize(void) const { return this->s; }

	void setSize(const Size &s_) {
		if(this->s != s_){
			this->s = s_;
			freeMemory();
			allocateMemory();
		}
	}

	void setData(const short unsigned int *d){
		if(d!=0 && this->depth!=0){
			memcpy(this->depth,d,this->s.width*this->s.height*sizeof(short unsigned int));
		}
	}

	short unsigned int * getData(void) { return this->depth; }
	const short unsigned int * getData(void) const { return this->depth; }

	short unsigned int getMax(void) const{
		return this->max_;
	}

	void setMax(const short unsigned int& v){
		this->max_ = v;
	}

private:

	void allocateMemory(){
		this->depth = new short unsigned int[this->s.width*this->s.height];
	}

	void freeMemory(){
		if(this->depth != 0)
			delete[] this->depth;
	}


private:

	short unsigned int *depth;
	Size s;
	short unsigned int max_;

};

}

#endif
